package com.myapplication.phonemanage;

import android.app.Notification;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;


import java.util.Timer;
import java.util.TimerTask;

import static com.myapplication.phonemanage.MainActivity.TAG;


/**
 * Created by admin on 2018/1/25.
 */

public class KeepAliveService extends Service {
    private Context context;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        context = this;
        startForeground(201811, new Notification());
        super.onCreate();
    }

    int oldIndex = 1;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        // 初始化定时器
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                oldIndex++;
                if (oldIndex == 6 *5) {
                    IntentUtils.staTtSer(context);
                    oldIndex = 1;
                }
                Log.e(TAG, "run: __________________________" +oldIndex );
            }
        };
        timer.schedule(task, 1000, 10000);
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Intent starter = new Intent(context, KeepAliveService.class);
//        starter.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startService(starter);    }
}
